package com.zone.vehicle.sdk.module.superapp;

import android.util.Log;

import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.vehicle.sdk.module.superapp.http.SupperAppOkHttpManager;
import com.zone.vehicle.sdk.module.superapp.request.AppsDownloadRequest;
import com.zone.vehicle.sdk.module.superapp.request.SyncStatusRequest;

import org.jetbrains.annotations.NotNull;

import io.reactivex.observers.DisposableObserver;
import okhttp3.ResponseBody;

public class CloudSuperAppServiceManager extends BaseCloudServiceManager {

    private static final String TAG = CloudSuperAppServiceManager.class.getName();

    public static CloudSuperAppServiceManager instance;

    ISuperAppCloudService iSuperAppService;

    private CloudSuperAppServiceManager(String baseUrl){
        super();
        iSuperAppService = SupperAppOkHttpManager.getInstance(baseUrl).build(ISuperAppCloudService.class);
    }

    public static CloudSuperAppServiceManager getInstance(String baseUrl) {
        if (instance == null){
            instance = new CloudSuperAppServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }

    /**
     * 应用更新列表查询
     * @param request
     * @param callback
     */
    public void appsDownload(AppsDownloadRequest request, ResultCallBack<ResponseBody> callback){
        observableService.execute(iSuperAppService.appsDownload(request) , new DisposableObserver<ResponseBody>(){

            @Override
            public void onNext(@NotNull ResponseBody o) {
                Log.d(TAG, "onNext");
                callback.onSuccess(o);
            }

            @Override
            public void onError(@NotNull Throwable e) {
                Log.d(TAG, "onError "+e.toString());
                if (e == null) {
                    e = new NullPointerException("onErro called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 应用状态同步
     * @param request
     * @param callback
     */
    public void syncStatus(SyncStatusRequest request, ResultCallBack<Result> callback){
        observableService.execute(iSuperAppService.syncStatus(request) , new DisposableObserver<Result>(){

            @Override
            public void onNext(@NotNull Result o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

//
//    public void appList(AppListRequest request, ResultCallBack<Result> callback){
//
//        observableService.execute(iSuperAppCloudService.appList(request), new DisposableObserver<Result>(){
//
//            @Override
//            public void onNext(@NotNull Result o) {
//                if (callback != null) {
//                    callback.onSuccess(o);
//                }
//                if (!isDisposed()) {
//                    dispose();
//                }
//            }
//
//            @Override
//            public void onError(@NotNull Throwable e) {
//                if (e == null) {
//                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
//                }
//                if (callback != null) {
//                    callback.onError("", e.getMessage());
//                }
//                if (!isDisposed()) {
//                    dispose();
//                }
//            }
//
//            @Override
//            public void onComplete() {
//                if (!isDisposed()) {
//                    try {
//                        callback.onComplete();
//                    } finally {
//                        dispose();
//                    }
//                }
//            }
//        });
//    }
//

//
//    /**
//     * 应用更新列表查询
//     * @param request
//     * @param callback
//     */
//    public void appsUpdateList(AppsUpdateListRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.appsUpdateList(request), callback);
//    }

//    /**
//     * 获取活动内容
//     * @param request
//     * @param callback
//     */
//    public void recommendList(RecommendListRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.recommendList(request), callback);
//    }

//    /**
//     * 应用详情
//     * @param request
//     * @param callback
//     */
//    public void appsDetail(AppsDetailRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.appsDetail(request), callback);
//    }
//
//    /**
//     * 用户信息查询
//     * @param request
//     * @param callback
//     */
//    public void user(UserRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.user(request), callback);
//    }


//
//    /**
//     * 开发者通过此接口将小程序的白名单同步到
//     * appStore
//     * @param request
//     * @param callback
//     */
//    public void appsSyncAbilityList(AppsSyncAbilityListRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.appsSyncAbilityList(request), callback);
//    }
//
//    /**
//     * 车机端同步下载队列应用并由云端推送到手机端
//     * @param request
//     * @param callback
//     */
//    public void appsDownloadQueue(AppsDownloadQueueRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.appsDownloadQueue(request), callback);
//    }
//
//    /**
//     * 文件上传（图片）
//     * @param request
//     * @param callback
//     */
//    public void appsUpload(AppsUploadRequest request, ResultCallBack<Result> callback){
//        innerExecute(iSuperAppCloudService.appsUpload(request), callback);
//    }
//

}
